package com.zhongpengcheng.blog.util;

import com.zhongpengcheng.blog.enums.ResultCodeEnum;
import com.zhongpengcheng.blog.exception.BizException;
import org.springframework.lang.Nullable;

/**
 * 参数校验工具
 * @author ZhongPengCheng
 * @version 1.0
 * @date 2021-11-25 20:27:00
 */
public class AssertUtil {
    /**
     * 非空校验
     * @param object 校验对象
     * @param resultCodeEnum 校验未通过时抛出的业务异常的枚举码
     */
    public static void notNull(@Nullable Object object, ResultCodeEnum resultCodeEnum) {
        if (object == null) {
            throw new BizException(resultCodeEnum);
        }
    }

    /**
     * 真值校验
     * @param condition 表达式
     * @param resultCodeEnum 校验未通过时抛出的业务异常的枚举码
     */
    public static void isTrue(boolean condition, ResultCodeEnum resultCodeEnum) {
        isFalse(!condition, resultCodeEnum);
    }

    /**
     * 假值校验
     * @param condition 表达式
     * @param resultCodeEnum 校验未通过时抛出的业务异常的枚举码
     */
    public static void isFalse(boolean condition, ResultCodeEnum resultCodeEnum) {
        if (condition) {
            throw new BizException(resultCodeEnum);
        }
    }
}
